package cj.web.portal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.paoding.rose.web.Invocation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.XMathUtil;
import cj.web.portal.dao.MemberVoDAO;
import cj.web.portal.dao.PayHistoryVoDAO;
import cj.web.portal.dao.PointConfigVoDAO;
import cj.web.portal.dao.VideoVoDAO;
import cj.web.portal.dao.WatchHistoryVoDAO;
import cj.web.yc.constants.PayHistoryStatusEnum;
import cj.web.yc.constants.PayHistoryTypeEnum;
import cj.web.yc.constants.VideoTypeEnum;
import cj.web.yc.domain.Video;

@Service
public class VideoVoService {
	private final static String VIDEO_URL="VIDEO_URL";
	
	@Autowired
    private VideoVoDAO videoDAO;
	@Autowired
    private PointConfigVoDAO pointConfigDAO;
	@Autowired
    private MemberVoDAO memberDAO;
	@Autowired
    private WatchHistoryVoDAO watchHistoryVoDAO;
	@Autowired
    private PayHistoryVoDAO payHistoryVoDAO;
	
	public List<Map> getVideoDetail(int videoId){
		return videoDAO.queryVideoDetail(videoId);
	}

	/**
	 * 查询
	 * @param video
	 * @return videolis;size
	 */
	public Map query(String categoryIds,int start,int length){
		Map rep = new HashMap();
		List<Map> videolis = videoDAO.queryVideolis(categoryIds,start,length);
		int size = videoDAO.queryVideoSize(categoryIds); 
		rep.put("videolis", videolis);
		rep.put("size", size);
		return rep;
    }
	
	/**
	 * 查询
	 * @param video
	 * @return videolis;size
	 */
	public Map queryVideoDetail(int videoId,String memberId,Invocation inv){
		Map rep = new HashMap();
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		
		if(videolis!=null && videolis.size()>0){
			//取加载的值
			ServletContext context = inv.getServletContext();  
	        HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
			Map configRep = (Map) dictionaryInfoMap.get("configRep");//配置
			String headUrl = configRep.get(VIDEO_URL).toString();
			
			if(VideoTypeEnum.SP.getValue()==Integer.parseInt(videolis.get(0).get("type").toString())){
				videolis.get(0).put("url", headUrl+videolis.get(0).get("url").toString());
			}
			
			rep.put("videoDetail", videolis.get(0));
			List<Map> videoDetailOtherlis = videoDAO.queryVideolis(videolis.get(0).get("categoryIds").toString());
			rep.put("videoDetailOtherlis", videoDetailOtherlis);
		}else{
			rep.put("videoDetail", "");
			rep.put("videoDetailOtherlis", "");
		}
		
		if(memberId!=null && !"".equals(memberId)){
			List<Map> memberlis = memberDAO.getMemberById(Integer.parseInt(memberId));
			if(memberlis!=null && memberlis.size()>0){
				rep.put("member", memberlis.get(0));
			}else{
				rep.put("member", null);
			}
		}else{
			rep.put("member", null);
		}
		
		return rep;
    }
	
	/**
	 * 查看状态
	 * @param memberId
	 * @param videoId
	 * @param status
	 * @return
	 */
	public List<Map> videoWatchHistory(int memberId,int videoId){
		return watchHistoryVoDAO.queryWatchHistorylis(memberId, videoId);
	}
	
	/**
	 * 视频播放
	 * @param videoId
	 * @param memberId
	 * @return
	 */
	public Map videoStart(int videoId,int memberId,int video_begin_code_pointnum,int categoryId){
		Map rep = new HashMap();
		rep.put("returnFlag", Constants.returnObj.returnFlag_true);
		//视频详情
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		List<Map> memberlis = memberDAO.getMemberById(memberId);
		
		if(memberlis==null || memberlis.size()==0){
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		if(videolis!=null && videolis.size()>0){
			int point = Integer.parseInt(memberlis.get(0).get("point").toString());
			//积分
			
			String resultValue = XMathUtil.add(String.valueOf(point), 
					String.valueOf(video_begin_code_pointnum));
			
			if(Integer.parseInt(resultValue)>=0){
				int size = watchHistoryVoDAO.querySize(memberId, videoId);
				if(size==0){
					memberDAO.updateMemberPoint(memberId, video_begin_code_pointnum);
					//插入观看历史记录
					watchHistoryVoDAO.create(memberId, videoId, categoryId);
					//插入消费历史
					payHistoryVoDAO.create(memberId, PayHistoryTypeEnum.XF.getValue(), video_begin_code_pointnum, "");
				}
				rep.put("returnCode", Constants.returnObj.returnCode.code_0);
			}else{
				rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
				rep.put("returnCode", Constants.returnObj.returnCode.code_5);
			}
			
		}else{
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		return rep;
	}
	
	
	/**
	 * 视频结束
	 * @param videoId
	 * @param memberId
	 * @return
	 */
	public Map videoEnd(int videoId,int memberId,Integer video_end_code_pointnum){
		Map rep = new HashMap();
		rep.put("returnFlag", Constants.returnObj.returnFlag_true);
		//视频详情
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		List<Map> memberlis = memberDAO.getMemberById(memberId);
		
		if(memberlis==null || memberlis.size()==0){
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		if(videolis!=null && videolis.size()>0){
			int size = watchHistoryVoDAO.querySize(memberId, 
					videoId, PayHistoryStatusEnum.BEGIN.getValue());
			if(size>0){
				memberDAO.updateMemberPoint(memberId, video_end_code_pointnum);
				//插入观看历史记录
				watchHistoryVoDAO.updateWatchHistoryForStatus(memberId, videoId);
				//插入消费历史
				payHistoryVoDAO.create(memberId, PayHistoryTypeEnum.ZS.getValue(), video_end_code_pointnum, "");
				
				rep.put("returnCode", Constants.returnObj.returnCode.code_0);
				return rep;
			}
			
			int size_end = watchHistoryVoDAO.querySize(memberId, 
					videoId, PayHistoryStatusEnum.END.getValue());
			if(size_end>0){
				rep.put("returnCode", Constants.returnObj.returnCode.code_1);
				return rep;
			}
		}else{
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		return rep;
	}
}